﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;

namespace UnitySheet
{
    /// <summary>
    /// 用于表格和XML文件的转换
    /// </summary>
    public class Sheet_XML : MonoBehaviour
    {

        public static void SheetToXML(Sheet sheet,XmlDocument xmlDoc, XmlElement mainElement)
        {
            List<Cell> cells = new List<Cell>();
            cells.AddRange(sheet.cells.Values);

            List<List<Vector3>> anchorPositions = new List<List<Vector3>>();
            for (int x = 0; x < sheet.anchors.Count; x++)
            {
                List<Vector3> positions = new List<Vector3>();
                for (int y = 0; y < sheet.anchors[x].Count; y++)
                {
                    positions.Add(sheet.anchors[x][y].transform.position);
                }
                anchorPositions.Add(positions);
            }

            List<int> index = new List<int>();
            foreach (var item in cells)
            {
                foreach (var i in item.anchorPoints)
                {
                    index.Add(i.x);
                    index.Add(i.y);
                }
            }

//            string filepath = Application.streamingAssetsPath + @"/my.xml";
            XmlElement xmlSheet = xmlDoc.CreateElement("sheet");
            //开始创建
            XmlElement anchorPoints = xmlDoc.CreateElement("anchorPoints");
            foreach (List<Vector3> item in anchorPositions)
            {
                XmlElement column = xmlDoc.CreateElement("column");
                foreach (var c in item)
                {
                    XmlElement position = xmlDoc.CreateElement("position");
                    XmlElement x = xmlDoc.CreateElement("x");
                    x.InnerText = c.x.ToString();
                    XmlElement y = xmlDoc.CreateElement("y");
                    y.InnerText = c.y.ToString();
                    XmlElement z = xmlDoc.CreateElement("z");
                    z.InnerText = c.z.ToString();
                    position.AppendChild(x);
                    position.AppendChild(y);
                    position.AppendChild(z);
                    column.AppendChild(position);
                }
                anchorPoints.AppendChild(column);
            }
            xmlSheet.AppendChild(anchorPoints);

            XmlElement cellIndex = xmlDoc.CreateElement("cellIndex");
            foreach (var item in index)
            {
                XmlElement oneIndex = xmlDoc.CreateElement("index");
                oneIndex.InnerText = item.ToString();
                cellIndex.AppendChild(oneIndex);
            }
            xmlSheet.AppendChild(cellIndex);
            mainElement.AppendChild(xmlSheet);
        }

        public static Sheet XMLToSheet(string xmlPath,string mainNodeName)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);
            XmlNode sheet = xmlDoc.SelectSingleNode(mainNodeName).SelectSingleNode("sheet");
            XmlNode anchors = sheet.SelectSingleNode("anchorPoints");
            XmlNode index = sheet.SelectSingleNode("cellIndex");

            List<List<Vector3>> anchorPositions = new List<List<Vector3>>();
            List<int> cellIndex = new List<int>();

            XmlNodeList column = anchors.SelectNodes("column");
            foreach (XmlElement item in column)
            {
                XmlNodeList positions = item.SelectNodes("position");
                List<Vector3> oneColumn = new List<Vector3>();
                foreach (XmlElement pos in positions)
                {
                    Vector3 newPos;
                    newPos.x = float.Parse(pos.GetElementsByTagName("x")[0].InnerText);
                    newPos.y = float.Parse(pos.GetElementsByTagName("y")[0].InnerText);
                    newPos.z = float.Parse(pos.GetElementsByTagName("z")[0].InnerText);
                    oneColumn.Add(newPos);
                }
                anchorPositions.Add(oneColumn);
            }

            foreach (XmlElement item in index)
            {
                cellIndex.Add(int.Parse(item.InnerText));
            }
            return SheetManager.sheetManager.CreateSheet(anchorPositions, cellIndex.ToArray());
        }
    }
}
